package com.jsb.controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.bokecc.bean.UploadReq;
import com.bokecc.bean.User;
import com.bokecc.bean.VerifyBean;
import com.bokecc.bean.VerifyRes;
import com.bokecc.bean.Video;
import com.bokecc.config.Config;
import com.jsb.common.utils.FileUtil;
import com.jsb.common.utils.JsonUtil;
import com.jsb.common.utils.Md5Util;
import com.jsb.common.utils.StringUtils;
import com.jsb.service.BokeccService;
import com.jsb.service.HttpClientService;

@Controller
@RequestMapping("bokecc")
public class BokeccController {
	private static final Logger log = Logger.getLogger(BokeccController.class);
	
	@Resource
	private HttpClientService httpClientService;
	
	@Resource
	private BokeccService bokeccService;
	/**
	 * 获取cc视频注册的用户信息
	 * @param model
	 * @return
	 */
	@RequestMapping("userinfo")
	public Object userinfo(Model model){
		User user = bokeccService.userinfo();
		model.addAttribute("userInfo", user);
		return "";
	}
	/**
	 * 获取单个视频信息
	 * @param model
	 * @param vedioId   视频id
	 * @return
	 */
	@RequestMapping("vedioinfo/{vedioId}")
	public Object vedioinfo(Model model,@PathVariable String vedioId){
		Video video = bokeccService.vedioinfo(vedioId);
		model.addAttribute("video", video);
		return null;
	}
	/**
	 * 上传视频
	 * @param model
	 * @param vedio
	 * @param uploadReq
	 * @return
	 */
//	@RequestMapping("vedio/upload")
	@ResponseBody
	public Object vedioUpload(MultipartFile vedio, @ModelAttribute UploadReq uploadReq){
		String md5 = null;
		File file = null;
		try {
			file = FileUtil.multipartToFile(vedio);
			md5 = Md5Util.getFileMD5String(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
		uploadReq.setMd5(md5);
		uploadReq.setFilesize(vedio.getSize());
		uploadReq.setFilename(vedio.getOriginalFilename());
		uploadReq.setFile(file);
//		bokeccService.vedioUpload2(uploadReq);
		return null;
	}
	
	
	/**
	 * 获取flash上传时用拼接的参数
	 * @param title
	 * @return
	 */
	@RequestMapping("getReqUrl")
	@ResponseBody
	public Object getReqUrl(String title){
		Map<String,String> map = new HashMap<String, String>();
		if(StringUtils.isNotEmpty(title)){
			map.put("title", title);
		}
		map.put("userid", Config.userid);
		String assemblyParam = bokeccService.assemblyParam(map);
		return assemblyParam;
	}
	@RequestMapping("verify")
	@ResponseBody
	public Object getVerrify(HttpServletRequest request,HttpServletResponse response){
		Enumeration enu=request.getParameterNames();  
		while(enu.hasMoreElements()){  
			String paraName=(String)enu.nextElement();  
			log.info("cc视频播放验证请求参数为:"+paraName+": "+request.getParameter(paraName));  
		}
		String verificationcode = request.getParameter("verificationcode");
		String vid = request.getParameter("vid");
		VerifyRes verify = bokeccService.verify(vid,verificationcode);
		verify.setVersion("1");
		Map<String,VerifyRes> map = new HashMap<String, VerifyRes>();
		map.put("response", verify);
		log.info(JsonUtil.toJson(map));
		return map;
	}
}
